Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Focus only elements on Tab keypress with "tabindex" property defined

I am developing a web application using Vue.js. I have a data entry form with 5 fields. The business need is on tab keypress, it should focus the first Input box and on subsequent tabs the next next element should be focused. I have archived using tabindex property to the elements.

Now the issue that I'm facing is after the form elements are over and when we hit tab it goes to the other elements where tabindex is not defined such as links and Browsers' URL bar.

What I want is if I have 6 elements (5 textbox + 1 submit button) and I have set tabindex from 1 to 6 than again after tab at element with tabindex 6 it should again focus on tabindex 1 and ignore other links and buttons on webpage.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can focus elements without the use of tabindex.

Vue.js allows to select DOM elements with this kind of syntax this.$refs.name (and if it doesn't work you can try this.$el.querySelector('yourElementSelector')

After selecting the element you can add focus() to it. For instance this.$refs.name.focus();

At your specific case, you can add this.$refs.name.focus() after an onclick event on the tab.

That will focus the element right after the click

about 4 years ago · Juan Pablo Isaza Relatório

0

You can prevent the focus outside tabIndex by place an empty span at the end of the last index element. Give it an id with tabindex = 0 along with an onfocus event, On trigger it help to focus to the first tabindex element.

<span tabindex="0" id="prevent-outside-focus" onfocus="foucsFirstTabIndex()"></span>

Live Demo :

new Vue({
  el: '#app',
  mounted() {
    document.getElementById('field1').focus();
  },
  methods: {
    foucsFirstTabIndex() {
        document.getElementById("field1").focus();
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input type="text" id="field1" tabindex="1"/><br>
  <input type="text" id="field2" tabindex="2"/><br>
  <input type="text" id="field3" tabindex="3"/><br>
  <input type="text" id="field4" tabindex="4"/><br>
  <button id="button" tabindex="5">Submit</button>
  <span tabindex="0" id="prevent-outside-focus" @focus="foucsFirstTabIndex()"></span>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

FYI - This is @RohitJindal's Vue 2 answer using Vue 3 Composition API

Modified so shift-tab on the first element moves to the last

Note: you'll need to renumber your tabindexes if you want to do it like this

const { createApp, onMounted } = Vue;
createApp({
    setup() {
        const focusFirst = () => {
            document.querySelector('[tabindex="2"]').focus();
        }
        const focusLast = () => {
            document.querySelector('[tabindex="6"]').focus();
        }
        onMounted(focusFirst);
        return { focusFirst, focusLast};
    }
}).mount('#app');
<script src="https://unpkg.com/vue@3.2.37/dist/vue.global.js"></script>
<div id="app">
  <span tabindex="1" @focus="focusLast"></span>
  <input type="text" tabindex="2"/><br>
  <input type="text" tabindex="3"/><br>
  <input type="text" tabindex="4"/><br>
  <input type="text" tabindex="5"/><br>
  <button id="button" tabindex="6">Submit</button>
  <span tabindex="7" @focus="focusFirst"></span>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda